<template>
	<view class="edit">
		<block v-if="!boxHide">
			<block v-if="type == 0">
				<view class="add_item">
					<view>产品名称</view>
					<view class="add_picker">{{info.title}}</view>
				</view>
				<view class="add_item">
					<view>产品编号</view>
					<view class="add_picker">{{info.number}}</view>
				</view>
				<view class="add_item">
					<view>当前库房</view>
					<picker mode="selector" :range="info.warehouse_list" :range-key="'title'" :data-name="'oldLibraryIndex'" @change="getPickerValue" class="add_picker">
						<text>{{info.warehouse_list[oldLibraryIndex].title}}</text>
						<image src="/static/images/right.png" class="right"></image>
					</picker>
				</view>
				<view class="add_item">
					<view>调整状态</view>
					<picker mode="selector" :range="moveList" :data-name="'moveListIndex'" @change="getPickerValue" class="add_picker">
						<text>{{moveList[moveListIndex]}}</text>
						<image src="/static/images/right.png" class="right"></image>
					</picker>
				</view>
				<view class="add_item">
					<view>库存</view>
					<view class="add_picker">{{info.stock}}</view>
				</view>
				<view class="add_item">
					<view>调拨至</view>
					<picker mode="selector" :range="library" :range-key="'title'" :data-name="'libraryIndex'" @change="getPickerValue" class="add_picker">
						<text>{{library[libraryIndex].title}}</text>
						<image src="/static/images/right.png" class="right"></image>
					</picker>
				</view>
				<view class="add_item">
					<view>入库状态</view>
					<picker mode="selector" :range="intoList" :data-name="'intoListIndex'" @change="getPickerValue" class="add_picker">
						<text>{{intoList[intoListIndex]}}</text>
						<image src="/static/images/right.png" class="right"></image>
					</picker>
				</view>
				<view class="add_item">
					<view>调拨数</view>
					<input type="number" v-model="stock" placeholder="请输入调拨数"/>
				</view>
				<view class="add_textarea">
					<view>入库备注</view>
					<textarea v-model="desc" placeholder="请输入入库备注" />
				</view>
			</block>
			
			<block v-if="type == 1">
				<view class="add_item">
					<view>入库价</view>
					<input type="number" v-model="price" placeholder="请输入入库价"/>
				</view>
			</block>
			
			<view class="save" @tap="submit()">确认</view>
		</block>
		<Loading ref="load"></Loading>
	</view>
</template>

<script>
	import Loading from "../../../components/loading/loading.vue"
	export default{
		components:{
			Loading
		},
		data(){
			return{
				laber:['测试1','测试2'],
				laberIndex:0,
				type:0,
				boxHide:true,
				info:{},
				library:[],
				libraryIndex:0,
				intoList:[],
				intoListIndex:0,
				moveList:[],
				moveListIndex:0,
				oldLibraryIndex:0,
				stock:"",
				desc:"",
				price:""
			}
		},
		onLoad(options) {
			this.type = Number(options.type);
			if(this.type == 1) {
				uni.setNavigationBarTitle({title:"调整入库价"});
				this.$refs.load.initLoading({loadType:1,loadPrompt:false}).then(res =>{
					this.boxHide = false;
					this.$api.getProductInfo({id:Number(options.id)}).then(res =>{
						this.info = res;
						this.$refs.load.getLoadingData({})
					})
				})
			}else{
				this.$api.getProductLibrary({id:Number(options.id)}).then(res =>{
					this.library = res
				})
				this.$api.getProductIntoList({status:0}).then(res =>{
					this.intoList = res
				})
				this.$api.getProductMoveList({id:Number(options.id)}).then(res =>{
					this.moveList = res
				})
				this.$refs.load.initLoading({loadType:1,loadPrompt:false}).then(res =>{
					this.boxHide = false;
					this.$api.getProductInfo({id:Number(options.id)}).then(res =>{
						this.info = res;
						this.$refs.load.getLoadingData({})
					})
				})
			}
		},
		methods:{
			getPickerValue(e){
				this[e.currentTarget.dataset.name] = e.detail.value;
			},
			submit(){
				if(this.type == 1){
					if(this.price <= 0){
						uni.showToast({title:"入库价不能为空",icon:"none"})
					}else{
						this.$api.changeProductPrice({id: this.info.id, price: this.price,cost_price:this.info.cost_price}).then(res => {
							this.$app.globalData.upData.product = true;
						})
					}
				}else{
					if(this.stock <= 0){
						uni.showToast({title:"调拨数不能少于0",icon:"none"})
					}else if(this.desc.length < 1){
						uni.showToast({title:"入库备注不能为空",icon:"none"})
					}else{
						this.$api.moveProduct({user_id:uni.getStorageSync("userId"),product_id:this.info.id,into_warehouse_id:this.library[this.libraryIndex].id,out_warehouse_id:this.info.warehouse_list[this.oldLibraryIndex].id,stock:Number(this.stock),change_status:this.moveList[this.moveListIndex],into_statuss:this.intoList[this.intoListIndex]}).then(res =>{
							this.$app.globalData.upData.product = true;
						})
					}
				}
			}
		}
	}
</script>

<style>
	.edit{width: 100%;background: #FFFFFF;padding-bottom: 50upx;}
</style>
